package com.ChenYaRou.www.controller;

import com.ChenYaRou.www.constant.JsonBeanConstant;
import com.ChenYaRou.www.dao.DistrictDao;
import com.ChenYaRou.www.dao.impl.DistrictDaoImpl;
import com.ChenYaRou.www.po.District;
import com.ChenYaRou.www.po.Street;
import com.ChenYaRou.www.utils.JsonBean;
import com.ChenYaRou.www.utils.UpdateOrInsertOrSelectMoreAllBean;

import java.sql.SQLException;
import java.util.List;

import static com.ChenYaRou.www.constant.False.EMPTY;

/**
 * @ClassName getDistrictServletController
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/4 15:21
 * @Version 1.0
 **/
public class GetDistrictServletController {
    /**
     * 获得所有地区district
     * @return
     */
    public String getAllDistrict(){
        List<District> districts = UpdateOrInsertOrSelectMoreAllBean.getInstance().selectMore(new District());
        return JsonBean.operateList(JsonBeanConstant.FAIL_UPDATE,districts);
    }

    /**
     * 获得地区对应的街区
     * @param districtName
     * @return
     */
    public String getTheStreet(String districtName){
        if(districtName!=null && !EMPTY.equals(districtName.trim())){
            DistrictDao districtDao=new DistrictDaoImpl();
            District district=new District();
            district.setName(districtName);
            try {
                District theDistrict = districtDao.selectSingle(district);
                if(theDistrict!=null){
                    Street street=new Street();
                    street.setDistrictId(theDistrict.getId());
                    List<Street> streets = UpdateOrInsertOrSelectMoreAllBean.getInstance().selectMore(street);
                    return JsonBean.operateList(JsonBeanConstant.FAIL_UPDATE,streets);
                }
            } catch (IllegalAccessException | SQLException | NoSuchFieldException | InstantiationException e) {
                e.printStackTrace();
            }
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }
}
